﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TracNghiemTinHocDaiCuong.BIZ;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.ThucThe;
using System.Text;
using System.Data;

namespace TracNghiemTinHocDaiCuong
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //RadioButtonList rdl = (RadioButtonList)Page.FindControl("rdlDapAn");
            //rdl.DataSource =   
        }

        protected void lvTest_PagePropertiesChanged(object sender, EventArgs e)
        {
            string selections = (ViewState["selections"] != null ? ViewState["selections"].ToString() : "");
            foreach (ListViewDataItem item in lvTest.Items)
            {
                RadioButton rdoDapAnA = (RadioButton)item.FindControl("rdoDapAnA");
                RadioButton rdoDapAnB = (RadioButton)item.FindControl("rdoDapAnB");
                RadioButton rdoDapAnC = (RadioButton)item.FindControl("rdoDapAnC");
                RadioButton rdoDapAnD = (RadioButton)item.FindControl("rdoDapAnD");
                HiddenField hfMaCH = (HiddenField)item.FindControl("MaCH");
                string MaCH = hfMaCH.Value;
                char dapAn = 'N';
                if (rdoDapAnA.Checked)
                {
                    dapAn = 'A';
                }
                else if (rdoDapAnB.Checked)
                {
                    dapAn = 'B';
                }
                else if (rdoDapAnC.Checked)
                {
                    dapAn = 'C';
                }
                else if (rdoDapAnD.Checked)
                {
                    dapAn = 'D';
                }
                else
                {
                    dapAn = 'N';
                }
                string pattern = string.Format("{0}=", MaCH);
                if (selections.Contains(pattern))
                {
                    int index = selections.IndexOf(pattern);
                    int replaceIndex = index + pattern.Length;
                    StringBuilder sb = new StringBuilder(selections);
                    sb[replaceIndex] = dapAn;
                    selections = sb.ToString();
                }
                else
                {
                    if (selections.Length == 0)
                    {
                        selections += MaCH + "=" + dapAn;
                    }
                    else
                    {
                        selections += "," + MaCH + "=" + dapAn;
                    }
                }
            }
            ViewState["selections"] = selections;
            lvTest.DataBind();
        }

        protected void dpTest_PreRender(object sender, EventArgs e)
        {
            int idDe = Methods.ConvertNumber(Session["maDe"].ToString());
            lvTest.DataSource = DeThi_CauHoiBIZ.LayDanhSach(idDe);
            lvTest.DataBind();
            string selections = (ViewState["selections"] != null ? ViewState["selections"].ToString() : "");
            foreach (ListViewDataItem item in lvTest.Items)
            {
                RadioButton rdoDapAnA = (RadioButton)item.FindControl("rdoDapAnA");
                RadioButton rdoDapAnB = (RadioButton)item.FindControl("rdoDapAnB");
                RadioButton rdoDapAnC = (RadioButton)item.FindControl("rdoDapAnC");
                RadioButton rdoDapAnD = (RadioButton)item.FindControl("rdoDapAnD");
                HiddenField hfMaCH = (HiddenField)item.FindControl("MaCH");
                string MaCH = hfMaCH.Value;
                string pattern = string.Format("{0}=", MaCH);
                if (selections.Contains(pattern))
                {
                    int index = selections.IndexOf(pattern);
                    int replaceIndex = index + pattern.Length;
                    char dapAn = selections[replaceIndex];
                    switch (dapAn)
                    {
                        case 'A':
                            rdoDapAnA.Checked = true;
                            break;
                        case 'B':
                            rdoDapAnB.Checked = true;
                            break;
                        case 'C':
                            rdoDapAnC.Checked = true;
                            break;
                        case 'D':
                            rdoDapAnD.Checked = true;
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        protected void btnGui_Click(object sender, EventArgs e)
        {
            LayBaiLam();
        }

        
        private void LayBaiLam()
        {
            List<string> rdoSelections = new List<string>();
            string selections = (ViewState["selections"] != null ? ViewState["selections"].ToString() : "");
            foreach (ListViewDataItem item in lvTest.Items)
            {
                RadioButton rdoDapAnA = (RadioButton)item.FindControl("rdoDapAnA");
                RadioButton rdoDapAnB = (RadioButton)item.FindControl("rdoDapAnB");
                RadioButton rdoDapAnC = (RadioButton)item.FindControl("rdoDapAnC");
                RadioButton rdoDapAnD = (RadioButton)item.FindControl("rdoDapAnD");
                HiddenField hfMaCH = (HiddenField)item.FindControl("MaCH");
                string MaCH = hfMaCH.Value;
                char dapAn = 'N';
                if (rdoDapAnA.Checked)
                {
                    dapAn = 'A';
                }
                else if (rdoDapAnB.Checked)
                {
                    dapAn = 'B';
                }
                else if (rdoDapAnC.Checked)
                {
                    dapAn = 'C';
                }
                else if (rdoDapAnD.Checked)
                {
                    dapAn = 'D';
                }
                else
                {
                    dapAn = 'N';
                }
                string pattern = string.Format("{0}=", MaCH);
                if (selections.Contains(pattern))
                {
                    int index = selections.IndexOf(pattern);
                    int replaceIndex = index + pattern.Length;
                    StringBuilder sb = new StringBuilder(selections);
                    sb[replaceIndex] = dapAn;
                    selections = sb.ToString();
                }
                else
                {
                    if (selections.Length == 0)
                    {
                        selections += MaCH + "=" + dapAn;
                    }
                    else
                    {
                        selections += "," + MaCH + "=" + dapAn;
                    }
                }
            }

            //Thêm kết quả bài làm của người dùng
            int idNguoiDung = (int)Session["idNguoiDung"];
            int maDe = Methods.ConvertNumber(Session["maDe"].ToString());

            DataTable table = DeThi_CauHoiBIZ.LayDanhSach(maDe);
            
            foreach (DataRow row in table.Rows)
            {
                string MaCH = row["MaCH"].ToString();
                string pattern = string.Format("{0}=", MaCH);
                if (selections.Contains(pattern))
                {
                    int index = selections.IndexOf(pattern);
                    int replaceIndex = index + pattern.Length;
                    rdoSelections.Add(selections[replaceIndex].ToString());
                }
                else
                {
                    rdoSelections.Add("N");
                }
            }

            BaiLam baiLam = new BaiLam();
            baiLam.MaND = idNguoiDung;
            baiLam.MaDT = maDe;
            baiLam.DapAnND = string.Join("", rdoSelections.ToArray());
            //BaiLamBIZ.Them(baiLam);
            IList<string> danhSachDapAn = DeThi_CauHoiBIZ.LayKetQua(maDe);
            lblKetQua.Text = TinhDiemBaiLam(danhSachDapAn, rdoSelections).ToString();
        }

        private float TinhDiemMoiCau(int soCauHoi)
        {
            return 10 / (float)soCauHoi;
        }

        private double TinhDiemBaiLam(IList<string> danhSachDapAn, IList<string> danhSachChon)
        {
            double diem = 0;
            for (int i = 0; i < danhSachChon.Count; i++)
            {
                if (danhSachChon[i].ToString() == danhSachDapAn[i].ToString())
                {
                    diem += TinhDiemMoiCau(20);
                }
            }
            return diem;
        }
    }
}